﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace PASNew.U.Controls
{
    public partial class NotificationsUC : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            DDNotifictions.SelectedIndexChanged += new EventHandler(DDNotifictions_SelectedIndexChanged);
            DDLeads.SelectedIndexChanged += new EventHandler(DDLeads_SelectedIndexChanged);
            if (!IsPostBack)
            {
                if (Session["UserIdentity"] != null)
                {
                    UserDetailsDto user = (UserDetailsDto)Session["UserIdentity"];

                    using (PASDataDataContext data = new PASDataDataContext())//loads all the differetn notifications is it correct? 
                    {
                        List<ListItem> contactNotifications = new List<ListItem>();
                        List<ListItem> leadsList = new List<ListItem>();

                        IEnumerable<int> clients = from c in data.ClientUserRelations
                                                   where c.UserId == user.UserID
                                                   select c.ClientId;
                        if (clients != null)
                        {
                            foreach (int i in clients)
                            {
                                var cDetails = (from c in data.ClientDetails
                                                where c.ClientId == i
                                                && c.Advertised == 1
                                                && c.EndDate <= DateTime.Now.AddDays(7)
                                                select c).FirstOrDefault();
                                if (cDetails != null)
                                {
                                    ListItem li = new ListItem
                                    {
                                        Text = cDetails.CompanyName,
                                        Value = cDetails.ClientId.ToString(),
                                    };

                                    contactNotifications.Add(li);
                                }

                                var clDetails = (from d in data.ClientDetails
                                                 where d.ClientId == i
                                                 && d.Advertised == 2
                                                 && d.ContactAgain <= DateTime.Now
                                                 select d).FirstOrDefault();

                                if (clDetails != null)
                                {
                                    ListItem lii = new ListItem
                                    {
                                        Text = clDetails.CompanyName,
                                        Value = clDetails.ClientId.ToString(),
                                    };

                                    contactNotifications.Add(lii);
                                }

                                var Leads = (from l in data.ClientDetails
                                             where l.ClientId == i
                                             && l.Advertised == 3
                                             select l).FirstOrDefault();

                                if (Leads != null)
                                {
                                    ListItem item = new ListItem
                                    {
                                        Text = Leads.CompanyName,
                                        Value = Leads.ClientId.ToString(),
                                    };

                                    leadsList.Add(item);
                                }
                            }

                            foreach (ListItem i in contactNotifications)
                            {
                                DDNotifictions.Items.Add(i);
                            }

                            DDLeads.DataSource = leadsList;
                            DDLeads.DataBind();
                        }
                    }
                }
                else
                {
                    Response.Redirect("Home.aspx");
                }
            }
        }

        void DDLeads_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (DDLeads.SelectedIndex != -1)
            {
                NotificationsDto CCompID = new NotificationsDto
                {
                    ClientCompName = DDLeads.SelectedItem.ToString(),
                };

                Session["LoadedCompanyName"] = CCompID;
                Response.Redirect("Clients.aspx");
            }
        }

        void DDNotifictions_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (DDNotifictions.SelectedIndex != -1)
            {
                NotificationsDto CCompID = new NotificationsDto
                {
                    ClientCompName = DDNotifictions.SelectedItem.ToString(),
                };

                Session["LoadedCompanyName"] = CCompID;
                Response.Redirect("Clients.aspx");
            }
        }

        protected void NCalender_SelectionChanged(object sender, EventArgs e)
        {
            DDNotifictions.Items.Clear();
            //DDLeads.Items.Clear();

            ListItem liPL = new ListItem
            {
                Value="-1",
                Text="Please select --",
            };

            DDNotifictions.Items.Add(liPL);
            DDNotifictions.AppendDataBoundItems = true;

            UserDetailsDto user = (UserDetailsDto)Session["UserIdentity"];

            using (PASDataDataContext data = new PASDataDataContext())
            {
                List<ListItem> contactNotifications = new List<ListItem>();

                IEnumerable<int> clients = from c in data.ClientUserRelations
                                           where c.UserId == user.UserID
                                           select c.ClientId;
                if (clients != null)
                {
                    foreach (int i in clients)
                    {
                        var cDetails = (from c in data.ClientDetails
                                        where c.ClientId == i
                                        && c.Advertised == 1
                                        && c.EndDate <= NCalender.SelectedDate.AddDays(7)
                                        select c).FirstOrDefault();
                        if (cDetails != null)
                        {
                            ListItem li = new ListItem
                            {
                                Text = cDetails.CompanyName,
                                Value = cDetails.ClientId.ToString(),
                            };

                            contactNotifications.Add(li);
                        }

                        var clDetails = (from d in data.ClientDetails
                                         where d.ClientId == i
                                         && d.Advertised == 2
                                         && d.ContactAgain <= NCalender.SelectedDate
                                         select d).FirstOrDefault();

                        if (clDetails != null)
                        {
                            ListItem lii = new ListItem
                            {
                                Text = clDetails.CompanyName,
                                Value = clDetails.ClientId.ToString(),
                            };

                            contactNotifications.Add(lii);
                        }
                    }
                }

                foreach (ListItem i1 in contactNotifications)
                {
                    DDNotifictions.Items.Add(i1);
                }
            }
        }
              
    }
}